package cn.edu.swust.adressbook.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.edu.swust.adressbook.R;
import cn.edu.swust.adressbook.adapter.ContactItemAdapter;
import cn.edu.swust.adressbook.bean.Contact;
import cn.edu.swust.adressbook.bean.Group;
import cn.edu.swust.adressbook.service.IBaseService;
import cn.edu.swust.adressbook.service.impl.ContactServiceImpl;


public class GroupDetailsActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String HINT = "获取群组详情失败";
    public static final String DELETE = "删除";
    public static final String CANCEL = "取消";
    public static final String EDIT = "编辑";
    public static final String GROUP = "群组";
    public static final String ADDRESS = "address";
    private IBaseService mContactsService;
    private List<Contact> mMembers;
    private TextView mGroupNameText;
    private ContactItemAdapter mItemAdapter;
    private Group mGroup;
    private ListView mListView;
    private TextView mCancelButton;
    private TextView mEditButton;
    private boolean mIsEditAll;
    public GroupDetailsActivity() {
        mIsEditAll = false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_details_list);
        getView();
        initData();
        setListener();
    }

    private void setListener() {
        findViewById(R.id.cancel_action).setOnClickListener(this);
        findViewById(R.id.send_messages_icon).setOnClickListener(this);
        findViewById(R.id.send_messages_text).setOnClickListener(this);
        findViewById(R.id.send_messages_layout).setOnClickListener(this);
        mEditButton.setOnClickListener(this);
        mCancelButton.setOnClickListener(this);
    }

    private void getView() {
        mGroupNameText = (TextView) findViewById(R.id.group_name);
        mListView = (ListView) findViewById(R.id.group_detail_items);
        mCancelButton = (TextView) findViewById(R.id.cancel_btn);
        mEditButton = (TextView) findViewById(R.id.edit_btn);
    }


    private void initData() {
        Intent intent = getIntent();
        mContactsService = new ContactServiceImpl(this);
        mItemAdapter = new ContactItemAdapter(GroupDetailsActivity.this);
        mMembers = new ArrayList<>();
        if (intent == null) {
            Toast.makeText(GroupDetailsActivity.this, HINT, Toast.LENGTH_SHORT).show();
            finish();
        } else {
            //获取传送的对象
            mGroup = (Group) intent.getSerializableExtra(GroupActivity.GROUP_ITEM_CHECKED);
            mGroupNameText.setText(mGroup.getName());
            updateMemberList();
        }
    }

    private void updateMemberList() {
        List<Contact> contacts = mContactsService.getAll();
        mMembers.clear();
        for (int i = 0; i < contacts.size(); i++) {
            Contact contact = contacts.get(i);
            if (Objects.equals(mGroup.getId(), contact.getGroupId())) {
                mMembers.add(contact);
            }
        }
        mItemAdapter.setNeedCheck(mIsEditAll);
        mItemAdapter.setContacts(mMembers);
        mListView.setAdapter(mItemAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_messages_icon:
            case R.id.send_messages_layout:
            case R.id.send_messages_text:
                sendMessages();
                break;
            case R.id.cancel_btn:
            case R.id.cancel_action:
                //TODO: to edit many list item
                comebackOrCancelDelete();
                break;
            case R.id.edit_btn:
                //TODO: intent add activity or delete
                editOrDelete();
                break;
            case R.id.all_button:
                break;
            default:
                break;

        }
    }

    private void sendMessages() {
        if(!mIsEditAll){
            String mobile = "";
            for (int i = 0; i < mMembers.size(); i++) {
                mobile += ";" + mMembers.get(i).getPhoneNumber();
            }
            mobile += ";" + "18300000000";
            mobile = mobile.substring(1, mobile.length());
            Log.i("GroupDetailsActivity",mobile);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.putExtra(ADDRESS, mobile);
            intent.setType("vnd.android-dir/mms-sms");
            startActivity(intent);
        }
    }

    private void editOrDelete() {

        mIsEditAll = !mIsEditAll;
        if (mIsEditAll) {
            mEditButton.setText(DELETE);
            mCancelButton.setText(CANCEL);
            updateMemberList();
        } else {
            long[] checkedItemIds = mListView.getCheckedItemIds();
            for (int i = 0; i < checkedItemIds.length; i++) {
                Contact contact = mMembers.get((int)checkedItemIds[i]);
                contact.setGroupId(null);
                mContactsService.update(contact);
            }
            restore();
        }

    }

    private void comebackOrCancelDelete() {

        if (mIsEditAll){
            mIsEditAll = false;
            restore();
        } else {
            finish();
        }
    }

    private void restore() {
        mEditButton.setText(EDIT);
        mCancelButton.setText(GROUP);
        updateMemberList();
    }
}